package org.hw.juc.thread;

import java.util.concurrent.*;

/**
 * 线程和线程池创建任务的方式
 */
public class ThreadAndPoolDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        System.out.println("------------------------- Thread -------------------------");

        /* MyThread extends Thread */
        MyThread mt=new ThreadAndPoolDemo.MyThread();
        mt.start();

        /* thread + runnable */
        Thread t=new Thread(()-> System.out.println("Thread + runnable"));
        t.start();

        /* Thread + FutureTask + callable */
        FutureTask task =new FutureTask(()->"Thread + FutureTask + callable");
        Thread tTask=new Thread(task);
        tTask.start();
        System.out.println(task.get());


        System.out.println("------------------------- ThreadPool -------------------------");
        ExecutorService pool= Executors.newCachedThreadPool();

        /* runnable */
        pool.execute(()-> System.out.println("ThreadPool + runable"));

        /* callable */
        Future<String> fcall=pool.submit(()->"ThreadPool + callable1");
        System.out.println(fcall.get());

        /* FutureTask + callable */
        FutureTask<String> taskCall=new FutureTask<>(()->"ThreadPool + FutureTask + callable");
        pool.submit(taskCall);
        System.out.println(taskCall.get());
    }
    static class MyThread extends Thread{
        @Override
        public void run() {
            System.out.println("MyThread extends Thread");
        }
    }
}
